package com.anycc.cloud.core.entity;

import com.anycc.cloud.common.entity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色实体类
 */
@Entity
@Table(
        name = "sys_role",
        indexes = {@Index(
                columnList = "name",
                name = "i_sys_role_name"
        ), @Index(
                columnList = "sn",
                name = "i_sys_role_sn"
        )}
)
public class Role extends BaseEntity {
    private static final long serialVersionUID = -8041198873127964622L;
    public static final String FIELD_NAME = "name";
    public static final String FIELD_SN = "sn";
    public static final String FIELD_DESCRIPTION = "description";
    public static final String FIELD_PERMISSIONS = "permissions";
    private String name;
    private String sn;
    private String description;
    private List<Permission> permissions = new ArrayList<>();

    public Role() {
    }

    @Column(
            name = "name",
            length = 64,
            nullable = false,
            unique = true
    )
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(
            name = "sn",
            length = 36,
            nullable = false,
            unique = true
    )
    public String getSn() {
        return this.sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    @Column(
            name = "description",
            length = 256
    )
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @JsonIgnore
    @ManyToMany
    @JoinTable(
            name = "sys_role_permission",
            joinColumns = {@JoinColumn(
                    name = "role_id",
                    referencedColumnName = "id"
            )},
            inverseJoinColumns = {@JoinColumn(
                    name = "permission_id",
                    referencedColumnName = "id"
            )}
    )
    @OrderBy("name DESC")
    public List<Permission> getPermissions() {
        return this.permissions;
    }

    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
    }
}
